package com.holly.unit.bpmn.modular.activiti.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.holly.unit.auth.api.context.LoginContext;
import com.holly.unit.auth.api.pojo.login.LoginUser;
import com.holly.unit.bpmn.activiti.ActivitiOperator;
import com.holly.unit.bpmn.activiti.entity.ActivitiZBusiness;
import com.holly.unit.bpmn.activiti.pojo.*;
import com.holly.unit.bpmn.api.pojo.BpmnUser;
import com.holly.unit.bpmn.modular.activiti.adapter.LoginUserToBpmnUser;
import com.holly.unit.core.pojo.response.ErrorResponseData;
import com.holly.unit.core.pojo.response.ResponseData;
import com.holly.unit.core.pojo.response.SuccessResponseData;
import com.holly.unit.db.api.pojo.page.PageResult;
import com.holly.unit.scanner.api.annotation.ApiResource;
import com.holly.unit.scanner.api.annotation.GetResource;
import com.holly.unit.scanner.api.annotation.PostResource;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * 类描述: 流程业务扩展信息
 *
 * @author fxr
 * @version 1.0.0
 * @date 2022/2/22 13:10
 */
@RestController
@ApiResource(name = "流程业务相关API")
@Api(tags = "流程业务相关API")
@Slf4j
public class ActProcessBusiController {

    @Resource
    private ActivitiOperator operator;

    @ApiOperation("添加申请草稿状态")
    @PostResource(name = "添加申请草稿状态", path = "/processBusi/add")
    public ResponseData add(@RequestBody @Validated(ProcessBusiAddDraftRequest.addDraft.class) ProcessBusiAddDraftRequest request){

        String procDefId = request.getProcDefId();
        String procDeTitle = request.getProcDeTitle();
        String tableName = request.getTableName();
        String filedNames = request.getFiledNames();
        Map<String,Object> formDataMap = request.getFormDataMap();

        //关联所属应用id
        String appId = request.getAppId();

        /*保存业务表单数据到数据库表*/
        String tableId = IdUtil.simpleUUID();

        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("procDefId",procDefId);
        paramMap.put("procDeTitle",procDeTitle);
        paramMap.put("tableName",tableName);
        paramMap.put("appId",appId);
        paramMap.put("filedNames",filedNames);
        paramMap.put("formDataMap",formDataMap);
        operator.addDraft(tableId, paramMap,request.getType());
        return new SuccessResponseData();
    }

    @ApiOperation("获取业务表单信息")
    @GetResource(name = "获取业务表单信息", path = "/processBusi/getForm")
    public ResponseData getForm(@Validated(ProcessBusiGetFormRequest.getFormByIdAndName.class) ProcessBusiGetFormRequest request){
        String tableId = request.getTableId();
        String tableName = request.getTableName();
        Map<String, Object> res = operator.fetchApplyForm(tableId, tableName);
        return new SuccessResponseData(res);
    }

    /*修改业务表单信息*/
    @ApiOperation("修改业务表单信息")
    @PostResource(name = "修改业务表单信息", path = "/processBusi/editForm")
    public ResponseData editForm(@RequestBody @Validated(ProcessBusiEditFormRequest.editForm.class) ProcessBusiEditFormRequest request) {

        String tableId = request.getTableId();
        String procDefId = request.getProcDefId();
        String procDeTitle = request.getProcDeTitle();
        String tableName = request.getTableName();
        String filedNames = request.getFiledNames();
        Map<String,Object> formDataMap = request.getFormDataMap();
        //关联所属应用id
        String appId = request.getAppId();
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("procDefId",procDefId);
        paramMap.put("procDeTitle",procDeTitle);
        paramMap.put("tableName",tableName);
        paramMap.put("appId",appId);
        paramMap.put("formDataMap",formDataMap);
        paramMap.put("filedNames",filedNames);
        operator.saveApplyForm(tableId,paramMap);
        return new SuccessResponseData();
    }

    @ApiOperation("通过id删除草稿状态申请")
    @PostResource(name = "通过id删除草稿状态申请", path = "/processBusi/delByIds")
    public ResponseData delByIds(@RequestBody  @Validated(ProcessBusiDelByIdsRequest.delByIds.class)ProcessBusiDelByIdsRequest request) {
        boolean b = operator.removeDraftByIds(request.getIds());
        if (b) {
            return new SuccessResponseData("删除成功");
        } else {
            return new ErrorResponseData("holly-d-bpmn","删除失败, 仅能删除草稿状态的申请");
        }
    }

    @ApiOperation("提交申请 启动流程")
    @PostResource(name = "提交申请 启动流程", path = "/processBusi/apply")
    public ResponseData apply(@RequestBody ProcessBusiApplyRequest request) {
        ActivitiZBusiness act = new ActivitiZBusiness();
        BeanUtil.copyProperties(request,act,false);
        // 当前登录用户
        LoginUser loginUser = LoginContext.me().getLoginUser();
        BpmnUser bpmnUser = LoginUserToBpmnUser.convert(loginUser);
        operator.apply(act,bpmnUser);
        return new SuccessResponseData();
    }

    @ApiOperation("撤回申请")
    @PostResource(name = "撤回申请", path = "/processBusi/cancel")
    public ResponseData cancel( @RequestBody @Validated(ProcessBusiCancelRequest.actCancel.class)  ProcessBusiCancelRequest request) {
        operator.cancel(request.getId(), request.getProcInstId(), request.getReason());
        return new SuccessResponseData();
    }

    @ApiOperation("流程业务列表")
    @GetResource(name = "流程业务列表", path = "/processBusi/listData")
    public ResponseData listData(ProcessBusiListRequest request) {

        ActivitiZBusiness param = new ActivitiZBusiness();
        BeanUtil.copyProperties(request,param,false);
        String appId = StrUtil.nullToDefault(request.getAppid(),"");
        String createTime_begin = StrUtil.nullToDefault(request.getCreateTimeBegin(),"");
        String createTime_end = StrUtil.nullToDefault(request.getCreateTimeEend(),"");
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("appId",appId);
        paramMap.put("createTime_begin",createTime_begin);
        paramMap.put("createTime_end",createTime_end);
        PageResult<ActivitiZBusiness> resList = operator.fetchActZBusinessList(param, paramMap);
        return new SuccessResponseData(resList);
    }


    /*修改业务表单信息*/
    @ApiOperation("表单数据_操作")
    @PostResource(name = "表单数据_操作", path = "/processBusi/editFormData")
    public ResponseData editFormData(@RequestBody @Validated(ProcessBusiEditFormRequest.editFormData.class) ProcessBusiEditFormRequest request) {
        String tableId = request.getTableId();
        String tableName = request.getTableName();
        String filedNames = request.getFiledNames();
        Map<String,Object> formDataMap = request.getFormDataMap();
        //关联所属应用id
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("tableName",tableName);
        paramMap.put("formDataMap",formDataMap);
        paramMap.put("filedNames",filedNames);
        operator.editFormData(tableId,paramMap);
        return new SuccessResponseData();
    }

}
